package wassalt.client;

import wassalt.Services.*;
import wassalt.Utils.AppConstants;
import wassalt.Utils.ClientServerCommunication;
import wassalt.Utils.User;
import android.os.Bundle;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;

public class NotificationsListActivity extends TabActivity {

	/**
	 * Called when the activity is first created.
	 * 
	 * @author Aya Kamel
	 * */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//If the user has not registered for the service direct him/her
		//to the registration form
		boolean registered = checkRegistered();
		if (!registered) {
			
			Log.e("onCreate","Not registered");
			Intent intent = new Intent();
			intent = new Intent().setClass(this, RegistrationActivity.class);
			startActivityForResult(intent, 0);
			finish();
			
		}
		
		//Show a notification if any
		if(getIntent().hasExtra("Notif_title") && getIntent().hasExtra("Notif_text"))
		{
			Bundle b = getIntent().getExtras();
			String title = b.getString("Notif_title");
			String text = b.getString("Notif_text");
			getIntent().removeExtra("Notif_title");
			getIntent().removeExtra("Notif_text");
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(title)
				   .setMessage(text)
			       .setCancelable(false)
			       .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
			           public void onClick(DialogInterface dialog, int id) {
			                //will do something
			           }
			       });
			AlertDialog alert = builder.create();
			alert.show();
		}
		
		//Set the layout for the activity
		setContentView(R.layout.notifications_list);
		setTitle(getString(R.string.notifications_title));

		Resources res = getResources();
		TabHost tabHost = getTabHost();
		TabHost.TabSpec spec;
		
		Intent intent1 = new Intent().setClass(this, RequestsListActivity.class);
		Bundle b = new Bundle();
		//Create the tabs which will hold the actual activities
		b.putInt("tab_id", AppConstants.TAB_INCOMING);
        intent1.putExtras(b);
		spec = tabHost
				.newTabSpec("Incoming")
				.setIndicator(res.getString(R.string.incoming),
						res.getDrawable(R.drawable.arrow_down_48))
				.setContent(intent1);
		tabHost.addTab(spec);

		Intent intent2 = new Intent().setClass(this, RequestsListActivity.class);
		b.putInt("tab_id", AppConstants.TAB_OUTGOING);
		intent2.putExtras(b);
		spec = tabHost
				.newTabSpec("Outgoing")
				.setIndicator(res.getString(R.string.outgoing),
						res.getDrawable(R.drawable.arrow_up_48))
				.setContent(intent2);
		tabHost.addTab(spec);
		
		Intent intent3 = new Intent().setClass(this, RequestsListActivity.class);
		b.putInt("tab_id", AppConstants.TAB_SELF);
		intent3.putExtras(b);
		spec = tabHost
				.newTabSpec("Self")
				.setIndicator(res.getString(R.string.self),
						res.getDrawable(R.drawable.bell))
				.setContent(intent3);
		tabHost.addTab(spec);

		tabHost.setCurrentTab(0);
		
		// ----Loai stuff-----
		// start the GPS service
		try {
			Intent gpsIntent = new Intent(this, GpsService.class);
			startService(gpsIntent);
		} catch (Exception e) {
			Log.e("GPSServiceLaunchError", e.getMessage());
		}
		// ----end Loai stuff----
		ClientServerCommunication csc = new ClientServerCommunication(this);
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode==RESULT_CANCELED)
			{finish();
			Log.d("back from Reg","propagated");
			}
		
		Bundle b = new Bundle();
		this.onCreate(b);
		
	}

	private boolean checkRegistered() {
		return User.isRegistered(this);
	}
}